package components {
import core.AGameObject;
import events.AreaSelectedEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import ru.cjay.loading.ResourceManager;
import ru.cjay.loading.UnsafeConnection;
import ui.ZoneInfoPopup;
import utils.TraceObject;


public class AreaItem extends AGameObject {

    private var disabled:Boolean;
    private var _is_open:Boolean = false;
    private var _drops:Array = null;
    private var _setting_path:String = null;


//    private var caption:TextField;
    private var _area:MovieClip;

    private var _setting:int;
    private var _session_key:int;

    private var _zone_id:int;
    private var _game_id:int;
    private var _cash:int;
    private var _cash_full:String;
    private var _cash_short:String;
    private var _col:int;
    private var _col_full:String;
    private var _col_short:String;
    private var _jackpot:int;
    private var _jackpot_full:String;
    private var _jackpot_short:String;
    private var _jackpot_hint:String;
    private var _cash_cost:int;
    private var _cash_cost_full:String;
    private var _cash_cost_short:String;
    private var _col_cost:int;
    private var _col_cost_full:String;
    private var _col_cost_short:String;
    private var _prize:Object;
    private var _size_x:int;
    private var _size_y:int;
    private var _winner:Object;
    private var _rating_clicks:Object;
    private var _rating_wins:Object;
    private var _click_result:int;
    private var _next_update:int;
    private var _cells:Array;

    private var _caption:String;

    private var _strings_user_count_hint:String;
    private var _strings_user_count:String;
    private var _strings_user_count_suf:String;
    private var _strings_try_cost:String;
    private var _strings_cash_name:String;
    private var _strings_col_name:String;
    private var _strings_in_cash:String;
    private var _strings_in_col:String;
    private var _strings_col_pre:String;
    private var _strings_quit_hint:String;
    private var _strings_click_result_hint:String;
    private var _strings_miss_hint:String;
    private var _strings_tell_friends_hint:String;
    private var _strings_wins_hint:String;
    private var _strings_clicks_hint:String;
    private var _strings_top_hint:String;


    public function get strings_user_count_hint():String {
        return _strings_user_count_hint;
    }

    public function set strings_user_count_hint(value:String):void {
        _strings_user_count_hint = value;
    }

    public function get strings_in_cash():String {
        return _strings_in_cash;
    }

    public function set strings_in_cash(value:String):void {
        _strings_in_cash = value;
    }

    public function get strings_in_col():String {
        return _strings_in_col;
    }

    public function set strings_in_col(value:String):void {
        _strings_in_col = value;
    }

    public function get strings_col_pre():String {
        return _strings_col_pre;
    }

    public function set strings_col_pre(value:String):void {
        _strings_col_pre = value;
    }

    public function get strings_miss_hint():String {
        return _strings_miss_hint;
    }

    public function set strings_miss_hint(value:String):void {
        _strings_miss_hint = value;
    }

    public function get strings_wins_hint():String {
        return _strings_wins_hint;
    }

    public function set strings_wins_hint(value:String):void {
        _strings_wins_hint = value;
    }

    public function get strings_clicks_hint():String {
        return _strings_clicks_hint;
    }

    public function set strings_clicks_hint(value:String):void {
        _strings_clicks_hint = value;
    }

    public function get strings_top_hint():String {
        return _strings_top_hint;
    }

    public function set strings_top_hint(value:String):void {
        _strings_top_hint = value;
    }

    public function get area():MovieClip {
        return _area;
    }

    public function set area(value:MovieClip):void {
        _area = value;
    }

    public function get strings_user_count():String {
        return _strings_user_count;
    }

    public function set strings_user_count(value:String):void {
        _strings_user_count = value;
    }

    public function get strings_user_count_suf():String {
        return _strings_user_count_suf;
    }

    public function set strings_user_count_suf(value:String):void {
        _strings_user_count_suf = value;
    }

    public function get strings_try_cost():String {
        return _strings_try_cost;
    }

    public function set strings_try_cost(value:String):void {
        _strings_try_cost = value;
    }

    public function get strings_cash_name():String {
        return _strings_cash_name;
    }

    public function set strings_cash_name(value:String):void {
        _strings_cash_name = value;
    }

    public function get strings_col_name():String {
        return _strings_col_name;
    }

    public function set strings_col_name(value:String):void {
        _strings_col_name = value;
    }

    public function get strings_quit_hint():String {
        return _strings_quit_hint;
    }

    public function set strings_quit_hint(value:String):void {
        _strings_quit_hint = value;
    }

    public function get strings_click_result_hint():String {
        return _strings_click_result_hint;
    }

    public function set strings_click_result_hint(value:String):void {
        _strings_click_result_hint = value;
    }

    public function get strings_tell_friends_hint():String {
        return _strings_tell_friends_hint;
    }

    public function set strings_tell_friends_hint(value:String):void {
        _strings_tell_friends_hint = value;
    }

    public function AreaItem(obj:Object = null) {
        this.buttonMode = true;
        this.useHandCursor = true;

        disabled = true;
//        alpha = 0.7;
        mouseChildren = false;
        _area = new (ResourceManager.getInstance().getSwfAssetClass("ui:ZoneBaloon") as Class)();
        addChild(_area);
        if (obj) {
            var icon:Sprite = UnsafeConnection.UnsafeLoader(obj.img, 80, 80);
            _area.icon.addChild(icon);
            _is_open = obj.is_open;
            _area.locked = !_is_open;
            _caption = obj.name;

            var setting_icon:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("icons:" + String(obj.setting)) as Class)();
            setting_icon.y = 45;
            _area.addChild(setting_icon);

            _zone_id = obj.zone_id;
            _setting = obj.setting;
            _setting_path = obj.setting_path;

            var hint:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("ui:ZoneHint") as Class )();
            hint.hint_caption = obj.name;
            hint.hint_desc = obj.description;

            var drops:Array = new Array();
            drops = drops.concat(obj.item_drops);
            drops = drops.concat(obj.specialitem_drops);

            var arr2:Array = [];
            // shuffling array
            while (drops.length > 0) {
                arr2.push(drops.splice(Math.round(Math.random() * (drops.length - 1)), 1)[0]);
            }
            drops = arr2;

//            hint.addDros(drops);

            // вытащил этод метод из компонента... туповато как-то, в будущем рефакторить по нормальному

            var last_y:int = 40;
            if (drops.length == 0) hint.dropList.visible = false;
            for each(var item:Object in drops) {

                var dr:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("ui:DropRow") as Class)();

                dr.caption.multiline = true;
                dr.caption.wordWrap = true;
                dr.caption.autoSize = "left";
                dr.caption.width = 178;
                dr.caption.text = item.name;

                dr.desc.multiline = true;
                dr.desc.wordWrap = true;
                dr.desc.autoSize = "left";
                dr.desc.width = 178;
                //TODO: Check here (description won`t toload');
//                trace("LOOK HERE! ->>>> ", TraceObject.traceObject(item.description));
                if (item["description"]!=null) dr.desc.text = item["description"];
                dr.desc.y = dr.caption.y + dr.caption.height;

                if (item.attack != null) {
                    dr.atack.text = item.attack;
                    dr.atack.y = dr.desc.y + dr.desc.height;
                } else {
                    dr.atack.visible = false;
                    dr.atack_name.visible = false;
                }
                if (item.defense != null) {
                    dr.defence.text = item.defense;
                    dr.defence.y = dr.atack.y;
                    dr.defence_name.y = dr.atack.y;
                    dr.atack_name.y = dr.atack.y;
                } else {
                    dr.defence.visible = false;
                    dr.defence_name.visible = false;
                }

                var icon:Sprite = UnsafeConnection.UnsafeLoader(item.img, 32, 32);
                dr.drop_icon.icon.addChild(icon);
//                dr.addChild(icon);
                dr.y = last_y;
                hint.dropList.addChild(dr);
                var y_inc:int = dr.desc.height + dr.desc.y + 35;
                if (dr.defence.visible == false) y_inc = y_inc - 10;
                if (y_inc < 60) y_inc = 60;
                last_y += y_inc;

            }
            hint.desc.y = hint.caption.y+hint.caption.height;
            hint.dropList.y = hint.desc.y+hint.desc.height+5;
            hint.back.height = hint.dropList.y+hint.dropList.height+15;

            // окончание вставленного из компонента кода


            setHintInstance(hint);
        }
//        addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
//        addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
        addEventListener(MouseEvent.CLICK, onClick);

    }

    private function set locked(val:Boolean):void {
        _area.locked = val;
    }

    private function get locked():Boolean {
        return _area.locked;
    }

    private function onPlayLevel(event:MouseEvent):void {
        if (event.target == event.currentTarget) {
            PlayThisLevel();
        }
    }

    public function PlayThisLevel():void {
        dispatchEvent(new AreaSelectedEvent(AreaSelectedEvent.AREA_PLAY));
    }


    public function select():void {
        parent.swapChildren(this, parent.getChildAt(parent.numChildren - 1));
        dispatchEvent(new AreaSelectedEvent(AreaSelectedEvent.AREA_SELECT));
    }

    private function onClick(event:MouseEvent):void {
        if (_area.locked) {
            ZoneInfoPopup.getInstance().show(_zone_id);
        }
        else {
            select();
        }
    }


    public function get zone_id():int {
        return _zone_id;
    }

    public function get setting():int {
        return _setting;
    }

//    private function onRollOver(event:MouseEvent):void {
//        TweenNano.to(_area, 0.25, {alpha:1});
//    }

//    private function onRollOut(event:MouseEvent):void {
//        TweenNano.to(_area, 0.25, {alpha:0.7});
//    }

    public function get session_key():int {
        return _session_key;
    }

    public function set session_key(value:int):void {
        _session_key = value;
    }

    public function fillAdditionInformation(obj:Object):void {
        // TODO: fill this information correctly!!!!!!!!!!!!!!
        _zone_id = obj.zone_id;
        _is_open = obj.is_open;
        _game_id = obj.game_id;


        _cash = obj.cash;
        _cash_full = obj.cash_full;
        _cash_short = obj.cash_short;
        _col = obj.col;
        _col_full = obj.col_full;
        _col_short = obj.col_short;
        _jackpot = obj.jackpot;
        _jackpot_full = obj.jackpot_full;
        _jackpot_short = obj.jackpot_short;
        _jackpot_hint = obj.jackpot_hint;
        _cash_cost = obj.cash_cost;
        _cash_cost_full = obj.cash_cost_full;
        _cash_cost_short = obj.cash_cost_short;
        _col_cost = obj.col_cost;
        _col_cost_full = obj.cost_full;
        _col_cost_short = obj.col_cost_short;
        _prize = obj._prize;
        _size_x = obj.size_x;
        _size_y = obj.size_y;

        _click_result = obj.click_result;
        _next_update = obj.next_update;

        if (obj.cells != null) {
            if (_cells == null) {
                _cells = new Array();
                _cells.length = _size_x;
            }
            for (var row in obj.cells) {
                if (_cells[row] == null) {
                    _cells[row] = new Array();
                    _cells[row].length = _size_y;
                }

                for (var pos_y in (obj.cells[row] as Array)) {
                    _cells[row][pos_y] = obj.cells[row][pos_y];
                }
            }
        }

        if (obj.winner != null) {
            _winner = new Object();
            _winner.user_id = obj.winner.user_id;
            _winner.fio = obj.winner.fio;
            _winner.avatar = obj.winner.avatar;
            _winner.win_date = obj.winner.win_date;
            _winner.win_x = obj.winner.win_x;
            _winner.win_y = obj.winner.win_y;
            _winner.hint = obj.winner.hint;
        }
        if (obj.prize != null) {
            _prize = new Object();
            _prize.prize_type = obj.prize.prize_type;
            _prize.item_id = obj.prize.item_id;
            _prize.name = obj.prize.name;
            _prize.description = obj.prize.description;
            _prize.attack = obj.prize.attack;
            _prize.defense = obj.prize.defense;
            _prize.img = obj.prize.img;
            _prize.itemtype_name = obj.prize.itemtype_name;
            _prize.hint = obj.prize.hint;

        }


        if (obj.rating_clicks != null) {
            _rating_clicks = new Array();
            for each(var it:Object in obj.rating_clicks) {
                _rating_clicks.push(it);
            }
        }
        if (obj.rating_wins != null) {
            _rating_wins = new Array();
            for each(var it:Object in obj.rating_wins) {
                _rating_wins.push(it);
            }
        }

        if (obj.strings != null) {
            _strings_user_count_hint = obj.strings.user_count_hint;
            _strings_user_count = obj.strings.user_count;
            _strings_user_count_suf = obj.strings.user_count_suf;
            _strings_try_cost = obj.strings.try_cost;
            _strings_cash_name = obj.strings.cash_name;
            _strings_col_name = obj.strings.col_name;
            _strings_in_cash = obj.strings.in_cash;
            _strings_in_col = obj.strings.in_col;
            _strings_col_pre = obj.strings.col_pre;
            _strings_quit_hint = obj.strings.quit_hint;
            _strings_click_result_hint = obj.strings.click_result_hint;
            _strings_miss_hint = obj.strings.miss_hint;
            _strings_tell_friends_hint = obj.strings.tell_friends_hint;
            _strings_wins_hint = obj.strings.wins_hint;
            _strings_clicks_hint = obj.strings.clicks_hint;
            _strings_top_hint = obj.strings.top_hint;
        }


    }

    public function get setting_path():String {
        return _setting_path;
    }

    public function set setting_path(value:String):void {
        _setting_path = value;
    }


    public function set zone_id(value:int):void {
        _zone_id = value;
    }

    public function get cash():int {
        return _cash;
    }

    public function set cash(value:int):void {
        _cash = value;
    }

    public function get cash_full():String {
        return _cash_full;
    }

    public function set cash_full(value:String):void {
        _cash_full = value;
    }

    public function get cash_short():String {
        return _cash_short;
    }

    public function set cash_short(value:String):void {
        _cash_short = value;
    }

    public function get col():int {
        return _col;
    }

    public function set col(value:int):void {
        _col = value;
    }

    public function get col_full():String {
        return _col_full;
    }

    public function set col_full(value:String):void {
        _col_full = value;
    }

    public function get col_short():String {
        return _col_short;
    }

    public function set col_short(value:String):void {
        _col_short = value;
    }

    public function get jackpot():int {
        return _jackpot;
    }

    public function set jackpot(value:int):void {
        _jackpot = value;
    }

    public function get jackpot_full():String {
        return _jackpot_full;
    }

    public function set jackpot_full(value:String):void {
        _jackpot_full = value;
    }

    public function get jackpot_short():String {
        return _jackpot_short;
    }

    public function set jackpot_short(value:String):void {
        _jackpot_short = value;
    }

    public function get cash_cost():int {
        return _cash_cost;
    }

    public function set cash_cost(value:int):void {
        _cash_cost = value;
    }

    public function get cash_cost_full():String {
        return _cash_cost_full;
    }

    public function set cash_cost_full(value:String):void {
        _cash_cost_full = value;
    }

    public function get cash_cost_short():String {
        return _cash_cost_short;
    }

    public function set cash_cost_short(value:String):void {
        _cash_cost_short = value;
    }

    public function get col_cost():int {
        return _col_cost;
    }

    public function set col_cost(value:int):void {
        _col_cost = value;
    }

    public function get col_cost_full():String {
        return _col_cost_full;
    }

    public function set col_cost_full(value:String):void {
        _col_cost_full = value;
    }

    public function get col_cost_short():String {
        return _col_cost_short;
    }

    public function set col_cost_short(value:String):void {
        _col_cost_short = value;
    }

    public function get prize():Object {
        return _prize;
    }

    public function set prize(value:Object):void {
        _prize = value;
    }

    public function get size_x():int {
        return _size_x;
    }

    public function set size_x(value:int):void {
        _size_x = value;
    }

    public function get size_y():int {
        return _size_y;
    }

    public function set size_y(value:int):void {
        _size_y = value;
    }

    public function get winner():Object {
        return _winner;
    }

    public function set winner(value:Object):void {
        _winner = value;
    }

    public function get rating_clicks():Object {
        return _rating_clicks;
    }

    public function set rating_clicks(value:Object):void {
        _rating_clicks = value;
    }

    public function get rating_wins():Object {
        return _rating_wins;
    }

    public function set rating_wins(value:Object):void {
        _rating_wins = value;
    }

    public function get next_update():int {
        return _next_update;
    }

    public function set next_update(value:int):void {
        _next_update = value;
    }

    public function get cells():Array {
        return _cells;
    }

    public function set cells(value:Array):void {
        _cells = value;
    }

    public function get jackpot_hint():String {
        return _jackpot_hint;
    }

    public function set jackpot_hint(value:String):void {
        _jackpot_hint = value;
    }

    public function get click_result():int {
        return _click_result;
    }

    public function set click_result(value:int):void {
        _click_result = value;
    }

    public function get game_id():int {
        return _game_id;
    }

    public function set game_id(value:int):void {
        _game_id = value;
    }

    public function get caption():String {
        return _caption;
    }

    public function set caption(value:String):void {
        _caption = value;
    }
}
}
